package com.edu.exam.entity.curriculum.questionbank;

import com.edu.exam.enums.curriculum.questionbank.QuestionBankType;
import com.edu.exam.enums.curriculum.questionbank.QuestionBankStatus;
import com.edu.exam.enums.curriculum.questionbank.QuestionBankAccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.Comment;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 题库实体类
 * 所属模块：B-02 题库管理模块
 * 对应数据库表：question_banks
 *
 * 管理题库的基本信息，包括题库编码、名称、类型、状态、访问级别等。
 * 支持题库的创建、编辑、审核、发布等全生命周期管理。
 * 关联科目、院系、负责人等关键信息，提供题库的完整视图。
 *
 * @author System
 * @version 1.0.0
 */
@Entity
@Comment("题库基础信息表")
@SQLDelete(sql = "UPDATE question_banks SET is_deleted = true, delete_time = CURRENT_TIMESTAMP WHERE id = ?")
@Where(clause = "is_deleted = false")
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class QuestionBank {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotBlank(message = "题库编码不能为空")
    @Size(max = 50, message = "题库编码长度不能超过50个字符")
    @Column(name = "bank_code", nullable = false, length = 50, unique = true)
    private String bankCode;

    @NotBlank(message = "题库名称不能为空")
    @Size(max = 200, message = "题库名称长度不能超过200个字符")
    @Column(name = "bank_name", nullable = false, length = 200)
    private String bankName;

    @Size(max = 1000, message = "题库描述长度不能超过1000个字符")
    @Column(name = "bank_description", length = 1000)
    private String bankDescription;

    @NotNull(message = "题库类型不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "bank_type", nullable = false, length = 30)
    private QuestionBankType bankType;

    @NotNull(message = "题库状态不能为空")
    @Enumerated(EnumType.STRING)
    @Column(name = "bank_status", nullable = false, length = 20)
    private QuestionBankStatus bankStatus;

    @NotNull(message = "学科领域不能为空")
    @Column(name = "subject_id", nullable = false)
    private Long subjectId;

    @Column(name = "subject_name", length = 100)
    private String subjectName;

    @Column(name = "department_id")
    private Long departmentId;

    @Column(name = "department_name", length = 100)
    private String departmentName;

    @Column(name = "difficulty_level")
    private Integer difficultyLevel;

    @Column(name = "question_count")
    @Builder.Default
    private Integer questionCount = 0;

    @Column(name = "active_question_count")
    @Builder.Default
    private Integer activeQuestionCount = 0;

    @Column(name = "total_questions")
    @Builder.Default
    private Integer totalQuestions = 0;

    @Column(name = "max_questions")
    private Integer maxQuestions;

    @Enumerated(EnumType.STRING)
    @Column(name = "access_level", length = 20)
    @Builder.Default
    private QuestionBankAccessLevel accessLevel = QuestionBankAccessLevel.PUBLIC;

    @Column(name = "is_shareable")
    @Builder.Default
    private Boolean isShareable = true;

    @Column(name = "is_template")
    @Builder.Default
    private Boolean isTemplate = false;

    @Column(name = "template_bank_id")
    private Long templateBankId;

    @Column(name = "tags", length = 1000)
    private String tags;

    @Column(name = "keywords", length = 1000)
    private String keywords;

    @Version
    @Column(name = "version", nullable = false)
    @Builder.Default
    private Integer version = 1;

    @Column(name = "is_deleted", nullable = false)
    @Builder.Default
    private Boolean isDeleted = false;

    @Column(name = "delete_time")
    private LocalDateTime deleteTime;

    @Size(max = 50, message = "创建人长度不能超过50个字符")
    @Column(name = "created_by", nullable = false, length = 50)
    private String createdBy;

    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    @Size(max = 50, message = "更新人长度不能超过50个字符")
    @Column(name = "updated_by", length = 50)
    private String updatedBy;

    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    // 关联关系
    @OneToMany(mappedBy = "questionBank", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<Question> questions;


    // ===== 枚举引用已迁移到独立枚举类 =====

}